package cn.org.xwb.bookmall.admin.service.impl;

import cn.org.xwb.bookmall.admin.service.AdminManagerService;
import cn.org.xwb.bookmall.model.po.AllowMail;
import cn.org.xwb.bookmall.model.po.Authority;
import cn.org.xwb.bookmall.model.service.impl.AllowMailServiceImpl;
import cn.org.xwb.bookmall.model.service.impl.AuthorityServiceImpl;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.page.dto.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AdminManagerServiceImpl implements AdminManagerService {

    @Autowired
    private AllowMailServiceImpl allowMailService;

    @Autowired
    private AuthorityServiceImpl authorityService;

    /**
     * 分页获取允许登录的邮箱
     *
     * @param current  第几页
     * @param pageSize 每个数据个数
     * @param disabled 是否禁用的邮箱
     * @param match    模糊匹配条件
     * @return
     */
    @Override
    public RespResult<Object> getAllowMailsPage(Integer current, Integer pageSize, Boolean disabled, String match) {
        PageResult<List<AllowMail>> allowMailsPage = allowMailService.findAllowMailsPage(current, pageSize, disabled, match);
        if (Objects.isNull(allowMailsPage)) return RespResult.fail("系统发生异常，请稍后重新尝试");
        return RespResult.success("获取允许登录管理平台的邮箱列表成功", allowMailsPage);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @Override
    public RespResult<Object> getAllAuthority() {
        List<Authority> authorities = authorityService.findAllFromCache();
        if (Objects.isNull(authorities)) return RespResult.fail("系统发生异常，请稍后程序尝试");
        return RespResult.success("获取权限列表成功", authorities);
    }

    /**
     * 分页获取权限列表
     *
     * @param current 第几页
     * @param pageSize 每页数据个数
     * @param disabled 是否禁用
     * @param nameMatch 权限名称匹配
     * @param codeMatch 权限编码匹配
     * @return
     */
    @Override
    public RespResult<Object> getAuthorityPage(Integer current, Integer pageSize, Boolean disabled, String nameMatch, String codeMatch) {
        PageResult<List<Authority>> authorityPage = authorityService.findAuthorityPage(current, pageSize, disabled, nameMatch, codeMatch);
        return RespResult.success("获取权限列表成功", authorityPage);
    }

    /**
     * 新增权限
     *
     * @param authority 新权限
     * @return
     */
    @Override
    public RespResult<Object> addAuthority(Authority authority) {
        RespResult<Object> respResult = judgeAuthorityExists(authority);
        // 权限已经存在
        if (Objects.nonNull(respResult)) return respResult;
        // 新增权限
        boolean saveRes = authorityService.save(authority);
        if (!saveRes) return RespResult.fail("新增权限失败，请稍后重新尝试");
        // 成功，刷新缓存
        authorityService.findAndCacheAll();
        return RespResult.success("新增权限成功");
    }

    /**
     * 修改权限
     *
     * @param authority
     * @return
     */
    @Override
    public RespResult<Object> updateAuthority(Authority authority) {
        RespResult<Object> respResult = judgeAuthorityExists(authority);
        // 权限存在
        if (Objects.nonNull(respResult)) return respResult;
        // 更新权限
        boolean updateRes = authorityService.updateById(authority);
        if (!updateRes) return RespResult.fail("权限修改失败，请稍后重新尝试");
        // 成功，刷新缓存
        authorityService.findAndCacheAll();
        return RespResult.success("权限修改成功");
    }

    /**
     * 判断权限是否存在
     *
     * @param authority
     * @return null-权限不存在，可以添加
     */
    private RespResult<Object> judgeAuthorityExists(Authority authority) {
        LambdaQueryWrapper<Authority> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Authority::getCode, authority.getCode()).or().eq(Authority::getName, authority.getName());
        Authority authority1 = authorityService.getOne(queryWrapper);
        if (Objects.nonNull(authority1)) {
            // 修改的为自己，可以不进行校验
            if (authority1.getId() == authority.getId()) return null;
            return RespResult.fail("该权限已经存在，请勿重复录入");
        }
        return null;
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @Override
    public RespResult<Object> delAuthority(Long id) {
        boolean res = authorityService.removeById(id);
        if (!res) return RespResult.fail("删除权限失败，请稍后重新尝试");
        // 成功，刷新缓存
        authorityService.findAndCacheAll();
        return RespResult.success("删除权限成功");
    }
}
